package de.koller.aframe.util.misc;

import de.koller.oceanFramework.util.GStat;
import de.koller.oceanFramework.util.MIn;
import de.koller.oceanFramework.util.MOut;

public class AStat extends GStat {

	public AStat joinAddIntern( String identifier, double value ) {
		super.joinAdd( identifier, value );
		return this;
	}

	public AStat joinMultiplyIntern( String identifier, double value ) {
		super.joinMultiply( identifier, value );
		return this;
	}
	
	public void write( MOut out ) {
		out.writeInt( additive.size() );
		for( String v : additive.keySet() ) {
			out.writeString( v );
			out.writeDouble( additive.get( v ) );
		}
		
		out.writeInt( mulitply.size() );
		for( String v : mulitply.keySet() ) {
			out.writeString( v );
			out.writeDouble( mulitply.get( v ) );
		}
	}
	
	public void read( MIn in ) {
		int j = in.readInt( 1 )[0];
		for( int i = 0; i < j; i++ )
			this.joinAdd( in.readString( 1 )[0], in.readDouble( 1 )[0] );
		
		j = in.readInt( 1 )[0];
		for( int i = 0; i < j; i++ )
			this.joinMultiply( in.readString( 1 )[0], in.readDouble( 1 )[0] );
	}

	@Override
	public String toString() {
		StringBuilder s = new StringBuilder( "AStat[ " );
		s.append( this.getValue() );
		s.append( " (" );
		
		s.append( " ADD: " );
		for( String v : additive.keySet() )
			s.append( v ).append( ";" ).append( additive.get( v ) ).append( " " );
		
		s.append( "MULT: " );
		for( String v : mulitply.keySet() )
			s.append( v ).append( ";" ).append( mulitply.get( v ) ).append( " " );
		
		s.append( ") ]" );
		return s.toString();
	}
	
	public static void main( String[] args ) {
		AStat s = new AStat().joinAddIntern( "default", 3 )
				.joinMultiplyIntern( "test", 2 )
				.joinAddIntern( "rer", 3.5 );
		
		MOut o = new MOut( "./temp/testStatSave.stat" );
		s.write( o );
		o.close();
		
		AStat s2 = new AStat();
		MIn i = new MIn( "./temp/testStatSave.stat" );
		s2.read( i );
		i.close();
		
		System.out.println( s.toString() );
		System.out.println( s2.toString() );
	}
}
